<#import "../desktop.ftl" as d>
<#import "../spring.ftl" as spring>

<#assign pageTitle>
	<@spring.message "signup.generic.page.title" />
</#assign>

<#assign showLanguageSelector=false />

<@d.desktop pageTitle showLanguageSelector>

	<h1>Registro de usuario</h1>
	
	<div id="contact_form">
		<form method="post" action="do_signup.html" id="main_form" name="main_form" onsubmit="return preparePassword();">
			<@spring.bind "signupForm.salt" />
			<input 
				type="hidden" 
				id="salt" 
				name="${spring.status.expression}"  
				value="${spring.status.value?default("")}" />
		
			<@spring.bind "signupForm.hashedPassword" />
			<input 
				type="hidden" 
				id="hashedPassword" 
				name="${spring.status.expression}"  
				value="${spring.status.value?default("")}" />
		
			<@spring.bind "signupForm.inviteHash" />
			<input 
				type="hidden" 
				autocomplete="off"
				id="inviteHash" 
				name="${spring.status.expression}"  
				value="${spring.status.value?default("")}" />
		
			<div>
				<label for="email"><@spring.message "signup.label.email" />:</label><br />
				<@spring.bind "signupForm.email" />
				<input 
					type="text" 
					autocomplete="off"
					id="email" 
					name="${spring.status.expression}"  
					value="${spring.status.value?default("")}" />
				<@spring.showErrors separator="|" classOrStyle="validation_error" />
				<#if signupForm.error>
					<span class="validation_error">${signupForm.errorMsg}</span>
				</#if>
			</div>
			<div style="margin-top: 20px;">
				<label for="locale"><@spring.message "signup.label.idioma" />:</label><br />
				<select id="locale" name="locale">
					<!-- <option value="ru">ru</option> -->
					<option value="es">es</option>
					<!-- <option value="en">en</option> -->
				</select>
			</div>
			<div style="margin-top: 20px;" class="passwd1">
				<label for="passwd1"><@spring.message "signup.label.contrasenya" />:</label><br />
				<input type="password" id="passwd1" name="passwd1" />
				<span class="validation_error"></span>
			</div>
			<div style="margin-top: 20px;">
				<label for="passwd2"><@spring.message "signup.label.repetir.contrasenya" />:</label><br />
				<input type="password" id="passwd2" name="passwd2" />
			</div>
			<div style="margin-top: 20px;">
				<button><@spring.message "signup.label.registrar" /></button>
			</div>
		</form>
	</div>
	
	<script type="text/javascript">
		function preparePassword() {
			var passwd1 = $("#passwd1").val();
			var passwd2 = $("#passwd2").val();
			
			if(8 > passwd1.length) {
				$(".passwd1 .validation_error").html("<@spring.message "signup.error.shortPassword" />");
				return false;
			}
			
			if(passwd1 != passwd2) {
				$(".passwd1 .validation_error").html("<@spring.message "signup.error.notSamePassword" />");
				return false;
			}
			
			var rnd = "";
			var i = passwd1.length;
			while(i != 0) {
				rnd += "0";
				i--;
			}
			
			$("#passwd1").val(rnd);
			$("#passwd2").val(rnd);
			
			var salt = $("#salt").val();
			
			var passwd = salt + passwd1 + salt;
			
			var shaObj = new jsSHA(passwd, "TEXT");
			var hashedPassword = shaObj.getHash("SHA-512", "HEX");
			
			$("#hashedPassword").val(hashedPassword);
			
			return true;
		}
	</script>
	
</@d.desktop>